package com.onsproject.nzbgetremote;

import java.net.URI;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.onsproject.json.xmlrpc.XMLRPCClient;
import com.onsproject.json.xmlrpc.XMLRPCException;

public class Settings {

	private final static String SETTINGS_PREF_NAME = "settings";

	private static Settings mSettings;

	private static String mIp = null;
	private static String mUserName = null;
	private static String mPassword = null;

	public static Settings getInstance(Context context) {

		SharedPreferences pref = context.getSharedPreferences(SETTINGS_PREF_NAME, Context.MODE_PRIVATE);

		mIp = pref.getString("ip", null);
		mUserName = pref.getString("username", null);
		mPassword = pref.getString("password", null);

		if (mSettings == null) {
			mSettings = new Settings();
		}
		return mSettings;
	}

	public Object callMethod(Context context, String method) {
		Object result = null;
		if ((mIp != null) && (mUserName != null) && (mPassword != null)) {
			XMLRPCClient client = new XMLRPCClient(URI.create("http://" + mIp + "/xmlrpc"), mUserName, mPassword);
			try {
				result = client.call(method);
			} catch (XMLRPCException e) {
				Log.e("NZBGetRemote", "Error while calling method: " + e.getMessage());
				return null;
			}
		}
		return result;
	}

	public Object callMethod(Context context, String method, Object param1, Object param2) {
		XMLRPCClient client = new XMLRPCClient(URI.create("http://" + mIp + "/xmlrpc"), mUserName, mPassword);
		Object result;
		try {
			result = client.call(method, param1, param2);
		} catch (XMLRPCException e) {
			Log.e("NZBGetRemote", "Error while calling method: " + e.getMessage());
			return null;
		}
		return result;
	}



	public void checkBeforeDialog(Context context) {
		if (mIp == null) {
			showDialog(context);
		}
	}

	public void showDialog(final Context context) {
		Builder builder = new Builder(context);

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.settings_dialog, null);

		builder.setView(v);
		builder.setInverseBackgroundForced(true);

		final AlertDialog alertdialog = builder.create();

		final EditText ipEdit = (EditText) v.findViewById(R.id.ip_edittext);
		final EditText usernameEdit = (EditText) v.findViewById(R.id.username_edittext);
		final EditText passwordEdit = (EditText) v.findViewById(R.id.password_edittext);

		ipEdit.setText(mIp);
		usernameEdit.setText(mUserName);
		passwordEdit.setText(mPassword);

		v.findViewById(R.id.test_button).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String ipaddress = ipEdit.getText().toString();
				String username = usernameEdit.getText().toString();
				String password = passwordEdit.getText().toString();

				XMLRPCClient client = new XMLRPCClient(URI.create("http://" + ipaddress + "/xmlrpc"), username, password);
				try {
					String version = (String) client.call("version");
					Toast.makeText(context, "Connection established.\nNZBGet server version " + version, Toast.LENGTH_SHORT).show();
				} catch (XMLRPCException e) {
					Toast.makeText(context, "No response received.\n[" + e.getMessage() + "]", Toast.LENGTH_SHORT).show();
				}
			}
		});
		v.findViewById(R.id.save_button).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SharedPreferences pref = context.getSharedPreferences(SETTINGS_PREF_NAME, Context.MODE_PRIVATE);

				SharedPreferences.Editor editor = pref.edit();
				mIp = ipEdit.getText().toString();
				mUserName = usernameEdit.getText().toString();
				mPassword = passwordEdit.getText().toString();
				editor.putString("ip", mIp);
				editor.putString("username", mUserName);
				editor.putString("password", mPassword);

				editor.commit();
				Toast.makeText(context, "Settings are saved!", Toast.LENGTH_SHORT).show();
				alertdialog.dismiss();
			}
		});
		alertdialog.show();
	}
}
